/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.sharding.sample.masterslave.service.impl;

import com.lvyh.lightframe.sharding.client.annotations.MultiDatasource;
import com.lvyh.lightframe.sharding.client.pagination.DefaultPageResult;
import com.lvyh.lightframe.sharding.client.pagination.PageParam;
import com.lvyh.lightframe.sharding.client.util.BeanUtil;
import com.lvyh.lightframe.sharding.sample.masterslave.bean.UserBean;
import com.lvyh.lightframe.sharding.sample.masterslave.bean.UserParamInfo;
import com.lvyh.lightframe.sharding.sample.masterslave.dao.entity.User;
import com.lvyh.lightframe.sharding.sample.masterslave.dao.mapper.UserMapper;
import com.lvyh.lightframe.sharding.sample.masterslave.service.ShardingMainDataService;
import com.lvyh.lightframe.sharding.sample.masterslave.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lvyh 2021/03/21.
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private ShardingMainDataService shardingMainDataService;

    @MultiDatasource(value = "master")
    public Long save(UserBean userBean) throws Exception {
        return shardingMainDataService.saveUser(userBean);
    }

    @MultiDatasource(value = "master")
    public boolean update(UserBean userBean) throws Exception {
        UserBean updateBean = shardingMainDataService.selectUserById(userBean.getId());
        if (updateBean == null) {
            return false;
        }
        BeanUtils.copyProperties(userBean, updateBean);
        return shardingMainDataService.updateUser(updateBean);
    }

    @MultiDatasource(value = "master")
    public boolean deleteUser(UserBean userBean) throws Exception {
        return shardingMainDataService.deleteUser(userBean.getId());
    }

    @MultiDatasource(value = "slave1")
    public UserBean selectById(UserBean userBean) throws Exception {
        return shardingMainDataService.selectUserById(userBean.getId());
    }

    @MultiDatasource(value = "master")
    public List<UserBean> selectListByPage(UserParamInfo param) throws Exception {
        UserBean condition = new UserBean();
        BeanUtils.copyProperties(param, condition);

        PageParam pageParam = new PageParam();
        pageParam.setPageNo(param.getPageNum());
        pageParam.setPageSize(param.getPageSize());
        //pageParam.setOrderColumns("update_time desc");
        DefaultPageResult<UserBean> pageInfo = shardingMainDataService.selectUserByCondition(condition, pageParam);

        if (pageInfo == null || CollectionUtils.isEmpty(pageInfo.getResultList())) {
            return null;
        }

        List<UserBean> result = pageInfo.getResultList();
        return result;
    }

    @MultiDatasource(value = "master")
    public List<UserBean> selectList(UserParamInfo param) throws Exception {
        UserBean condition = new UserBean();
        BeanUtils.copyProperties(param, condition);

        List<UserBean> result = shardingMainDataService.selectUser(condition);
        return result;
    }

}
